﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/12/3 0:04:19
 * ************************************************************/
namespace Mozlite.Common
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Collections.Generic;
    using Mozlite.Common.Configuration;

    /// <summary>
    /// 论坛管理类。
    /// </summary>
    public class Forums
    {
        private Forums() { }

        /// <summary>
        /// 保存论坛。
        /// </summary>
        /// <param name="forum">论坛实例对象。</param>
        /// <returns>返回论坛状态。</returns>
        public static CreateForumStatus SaveForum(Forum forum)
        {
            var status = ForumDataProvider.Current.SaveForum(forum);
            if (status == CreateForumStatus.Created || status == CreateForumStatus.Updated)
                Caches.Remove(GetCacheKey());
            return status;
        }

        /// <summary>
        /// 删除论坛。
        /// </summary>
        /// <param name="forumID">论坛ID。</param>
        /// <returns>返回论坛删除状态。</returns>
        public static CreateForumStatus DeleteForum(int forumID)
        {
            var status = ForumDataProvider.Current.DeleteForum(forumID, SiteSettings.Current.SettingsID);
            if (status == CreateForumStatus.Deleted)
                Caches.Remove(GetCacheKey());
            return status;
        }

        /// <summary>
        /// 加载论坛列表。
        /// </summary>
        /// <returns>返回论坛列表。</returns>
        public static Dictionary<string, Forum> LoadForums()
        {
            return Caches.Get<Dictionary<string, Forum>>(GetCacheKey(), Globals.MinuteSeconds * 3, Globals.MinuteSeconds * 3, () => {
                return ForumDataProvider.Current.LoadForums(SiteSettings.Current.SettingsID);
            });
        }

        /// <summary>
        /// 获取论坛实例对象。
        /// </summary>
        /// <param name="forumID">论坛ID。</param>
        /// <returns>返回论坛实例对象。</returns>
        public static Forum GetForum(int forumID)
        {
            return ForumDataProvider.Current.GetForum(forumID, null, SiteSettings.Current.SettingsID);
        }

        /// <summary>
        /// 获取论坛实例对象。
        /// </summary>
        /// <param name="key">论坛键。</param>
        /// <returns>返回论坛实例对象。</returns>
        public static Forum GetForum(string key)
        {
            return ForumDataProvider.Current.GetForum(-1, key, SiteSettings.Current.SettingsID);
        }

        /// <summary>
        /// 获取论坛实例对象。
        /// </summary>
        /// <param name="key">论坛键。</param>
        /// <returns>返回论坛实例对象。</returns>
        public static Forum GetForumFromCache(string key)
        {
            Forum forum;
            if (LoadForums().TryGetValue(key, out forum))
                return forum;
            return null;
        }

        /// <summary>
        /// 获取论坛实例对象。
        /// </summary>
        /// <param name="forumID">论坛ID。</param>
        /// <returns>返回论坛实例对象。</returns>
        public static Forum GetForumFromCache(int forumID)
        {
            return (from forum in LoadForums().Values
                    where forum.ForumID == forumID
                    select forum).SingleOrDefault<Forum>();
        }

        private static string GetCacheKey()
        {
            return string.Format("AllForum::SettingsID.{0}", SiteSettings.Current.SettingsID);
        }
    }
}